package org.example.studyonline.controller;

import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.example.studyonline.pojo.DTO.*;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.Result;
import org.example.studyonline.pojo.VO.UserListVo;
import org.example.studyonline.service.ChapterService;
import org.example.studyonline.service.CourseMaterialService;
import org.example.studyonline.service.CourseService;
import org.example.studyonline.service.UserService;
import org.example.studyonline.utils.AliOssUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private CourseService courseService;

    @Resource
    private UserService userService;

    @Resource
    private ChapterService chapterService;

    @Resource
    private CourseMaterialService courseMaterialService;


    // 新增课程
    @RequiresAuthentication
    @RequiresRoles("2")
    @PutMapping("/course")
    public Result course(
            @RequestParam("name") String name,
            @RequestParam("desc") String description,
            @RequestParam("img") MultipartFile img,
            @RequestParam("price") Float price   ) throws Exception {

        String OriginalFilename = img.getOriginalFilename();
        String filename = UUID.randomUUID().toString()+OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
        String url = AliOssUtil.uploadFile(filename, img.getInputStream());

        AddCourseDTO addCourseDTO = new AddCourseDTO();
        addCourseDTO.setName(name);
        addCourseDTO.setDescription(description);
        addCourseDTO.setImg(url);
        addCourseDTO.setPrice(price);
        courseService.add(addCourseDTO);
        return Result.success(null);
    }


    // 修改课程信息
    @RequiresAuthentication
    @RequiresRoles("2")
    @PostMapping("/course")
    public Result course(
            @RequestParam(value = "courseID",required = true) String courseID,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "desc",required = false) String description,
            @RequestParam(value = "img",required = false) MultipartFile img,
            @RequestParam(value = "price",required = false) Float price   ) throws Exception {
        String url = null;
        if(!img.isEmpty()){
            String OriginalFilename = img.getOriginalFilename();
            String filename = UUID.randomUUID().toString() + OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
            url = AliOssUtil.uploadFile(filename, img.getInputStream());
        }
        UpdateCourseDTO updateCourseDTO = new UpdateCourseDTO();
        updateCourseDTO.setCourseID(Integer.parseInt(courseID));
        updateCourseDTO.setName(name);
        updateCourseDTO.setDescription(description);
        updateCourseDTO.setImg(url);
        updateCourseDTO.setPrice(price);

        courseService.update(updateCourseDTO);
        return Result.success(null);
    }


    // 删除课程信息
    @RequiresAuthentication
    @RequiresRoles("2")
    @DeleteMapping("/course")
    public Result course(@RequestBody @Validated DeleteCourseDTO deleteCourseDTO){
        courseService.delete(deleteCourseDTO);
        return Result.success(null);
    }

    @RequiresRoles("3")
    @PutMapping("/user/freeze/{userID}")
    public Result freezeUserByUserID(@PathVariable Integer userID){
        userService.freezeUserByUserID(userID);
        return Result.success("用户冻结成功",null);
    }

    @RequiresAuthentication
    @RequiresRoles("2")
    @PostMapping("/course/chapter")
    public Result addChapter(@RequestBody @Validated AddChapterDTO addChapterDTO){
        chapterService.add(addChapterDTO);
        return Result.success("章节添加成功",null);
    }

    @RequiresAuthentication
    @RequiresRoles("3")
    @GetMapping("/user/list")
    public Result<PageBean<UserListVo>> getUserList(@RequestParam(value = "page", defaultValue = "1") Long pageNo, @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize){
        PageBean<UserListVo> list = new PageBean<>();
        list =userService.getUserList(pageNo,pageSize);
        if (list == null) {
            return Result.error("获取用户列表失败");
        }
        return Result.success("获取用户列表成功",list);
    }

    @RequiresAuthentication
    @RequiresRoles("4")
    @GetMapping("/course/statistics")
    public Result<PageBean<CategoryCountDTO>> getStatistics(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pageSize", defaultValue = "5") Long size){
        PageBean<CategoryCountDTO> list = new PageBean<>();

        list = courseService.getCategoryCount(page,size);

        if(list == null){
            return Result.error("获取数据失败");
        }
        return Result.success(list);
    }

    @RequiresAuthentication
    @RequiresRoles("4")
    @GetMapping("/course/watch-count")
    public Result<PageBean<WatchCountDTO>> getWatchCount(@RequestParam(value = "page", defaultValue = "1") Long page, @RequestParam(value = "pageSize", defaultValue = "5") Long size){
        PageBean<WatchCountDTO> list = new PageBean<>();

        list = courseService.getWatchOut(page,size);

        if(list == null){
            return Result.error("获取数据失败");
        }
        return Result.success(list);
    }

    @RequiresAuthentication
    @RequiresRoles("2")
    @PostMapping("/chapter/material/put")
    public Result putMaterial(@RequestParam(value = "chapterID", required = true) Integer chapterID,@RequestParam(value = "type", required = true)String type,@RequestParam(value = "filePath", required = true) MultipartFile filePath) throws Exception {
        courseMaterialService.putMaterial(chapterID,type,filePath);
        return Result.success();
    }

    @RequiresAuthentication
    @RequiresRoles("2")
    @PostMapping("/chapter/material/update")
    public Result updateMaterial(@RequestParam("materialID")Integer materialID,@RequestParam("chapterID") Integer chapterID,@RequestParam("type")String type,@RequestParam("filePath")MultipartFile filePath) throws Exception {
        courseMaterialService.updateMaterial(materialID,chapterID,type,filePath);
        return Result.success();
    }

    @RequiresAuthentication
    @RequiresRoles("2")
    @PostMapping("/chapter/material/delete")
    public Result deleteMaterial(@RequestParam("materialID")Integer materialID){
        courseMaterialService.removeById(materialID);
        return Result.success();
    }

    @RequiresAuthentication
    @RequiresRoles("2")
    @PutMapping("/user/unfreeze/{userID}")
    public Result unfreezeUserByUserID(@PathVariable Integer userID){
        userService.unfreezeUserByUserID(userID);
        return Result.success("用户解冻成功",null);
    }
}
